// member/pages/coupon/coupon.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let wxbarcode = require("../../../utils/wxbarcode/index")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 10,
    listData: [],
    status: 1,
    qrType: false
  },
  toOfflineOrderDetails(e) {
    wx.navigateTo({
      url: `/member/pages/offlineOrderDetails/offlineOrderDetails?order_sn=${e.target.dataset.order_sn}`,
    })
  },
  //显示二维码
  showQr(e) {
    const that = this;
    that.setData({
      qrcode: e.currentTarget.dataset.qrcode,
      qrType: true
    })
    setTimeout(() => {
      that.qrCode(that.data.qrcode)
    }, 200)
  },
  remQr() {
    const that = this;
    that.setData({
      qrType: false
    })
  },
  qrCode(code) {
    const query = this.createSelectorQuery()
    query.select('#qrcode').fields({
      node: true,
      size: true
    }).exec(res => {
      const canvas = res[0].node
      wxbarcode.wxqrcode({
        canvas: canvas,
        canvasId: 'qrcode',
        width: 200,
        padding: 50,
        paddingColor: '#fff',
        background: '#fff',
        foreground: '#000000',
        text: code,
      });
    })
  },
  navClick(e) {
    let that = this;
    if (e.target.dataset.status == that.data.status) {
      return;
    }
    that.setData({
      status: e.target.dataset.status,
      page: 1,
      isEnd: true,
      isWithout: true,
      listData: []
    })
    that.getData()
  },
  getData() {
    const that = this;
    let data = {
      page: that.data.page,
      ali_appid:'',
      ali_openid:'',
      order_type: parseInt(that.data.status)
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.offlineOrderList, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.list.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {

          that.setData({
            isWithout: true,
            listData: res.data.result.list
          })
        }
      } else {
        that.setData({
          listData: [],
          isWithout: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
    })

    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      page: that.data.page + 1,
      ali_appid:'',
      ali_openid:'',
      order_type: parseInt(that.data.status)
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.offlineOrderList, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.list.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {

          that.setData({
            listData: [...that.data.listData, ...res.data.result.list],
            page: that.data.page + 1
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})